<template>
  <div class="index-container">
    <!--搜索-->
    <div class="search-box">
      <van-search v-model="searchVal" placeholder="请输入搜索关键字" disabled @click="$router.push('/searchPopup').catch(err => {})" />
    </div>
    <!--搜索弹出-->
    <transition name="van-slide-right">
      <router-view></router-view>
    </transition>
    <!--轮播图banner-->
    <div class="index-banner">
      <van-swipe :autoplay="3000" indicator-color="#b75d12">
        <van-swipe-item v-for="item in banner" :key="item.id">
          <van-image fit="cover" :src="item.image_url" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!--频道-->
    <div class="index-channel">
      <van-grid :column-num="5">
        <van-grid-item v-for="item in channel" :key="item.id" @click="toList(item.id)">
          <van-image fit="cover" :src="item.icon_url" />
          <span class="text">{{item.name}}</span>
        </van-grid-item>
      </van-grid>
    </div>
    <!--品牌-->
    <div class="index-brand">
      <h2 class="title-h2">品牌制造商直供</h2>
      <ul>
        <li v-for="item in brand" :key="item.id" @click="toBrandList(item.id)"><div class="box">
          <van-image fit="cover" :src="item.list_pic_url" />
          <div class="tmain">
            <h2 class="tit">{{item.name}}</h2>
            <span class="price">{{item.floor_price | RMBformat}}</span>
          </div>
        </div></li>
      </ul>
    </div>
    <!--新品首发-->
    <div class="index-newGoods">
      <h2 class="title-h2">周一周四·新品首发</h2>
      <ul>
        <li v-for="item in newGoodsList" :key="item.id" @click="toProductsDetail(item.id)"><div class="box">
          <van-image fit="cover" :src="item.list_pic_url" />
          <h2 class="tit van-ellipsis">{{item.name}}</h2>
          <span class="price">{{item.retail_price | RMBformat}}</span>
        </div></li>
      </ul>
    </div>
    <!--人气推荐-->
    <div class="index-hotGoods">
      <h2 class="title-h2">人气推荐</h2>
      <ul>
        <li v-for="item in hotGoodsList" :key="item.id" @click="toProductsDetail(item.id)">
          <van-image fit="cover" :src="item.list_pic_url" />
          <div class="tmain">
            <h2 class="tit van-ellipsis">{{item.name}}</h2>
            <p class="summary">{{item.goods_brief}}</p>
            <span class="price">{{item.retail_price | RMBformat}}</span>
          </div>
        </li>
      </ul>
    </div>
    <!--专题精选-->
    <div class="index-special">
      <h2 class="title-h2">专题精选</h2>
      <div class="special-list">
        <van-swipe :loop="false" :width="300">
          <van-swipe-item v-for="item in topicList" :key="item.id">
            <van-image fit="cover" :src="item.item_pic_url" />
            <div class="tmain">
              <h2 class="tit van-ellipsis">{{item.title}}</h2>
              <p class="summary van-ellipsis">{{item.subtitle}}</p>
              <span class="price">{{item.price_info | RMBformat}}</span>
            </div>
          </van-swipe-item>
        </van-swipe>
      </div>
    </div>
    <!--分类板块-->
    <div class="index-category">
      <div class="list-item" v-for="list in categoryList" :key="list.id">
        <h2 class="title-h2">{{list.name}}</h2>
        <ul>
          <li v-for="item in list.goodsList" :key="item.id" @click="toProductsDetail(item.id)"><div class="box">
            <van-image fit="cover" :src="item.list_pic_url" />
            <h2 class="tit van-ellipsis">{{item.name}}</h2>
            <span class="price">{{item.retail_price | RMBformat}}</span>
          </div></li>
        </ul>
      </div>
    </div>
    <!--返回顶部-->
    <BackTop />
  </div>
</template>

<script>
import { GetHomeData } from '@/request/api'
import BackTop from '@/components/BackTop/BackTop.vue'
export default {
  name: 'Home',
  components: {
    BackTop
  },
  data () {
    return {
      searchVal: '', // 搜索框内容
      banner: [], // 轮播图数据
      channel: [], // 频道数据
      brand: [], // 品牌数据
      newGoodsList: [], // 新品首发数据
      hotGoodsList: [], // 人气推荐
      topicList: [], // 专题精选
      categoryList: [] // 分类板块
    }
  },
  created () {
    GetHomeData().then(res => {
      // 请求成功之后执行
      // console.log('----then中的res是：', res)
      // 轮播图banner数据
      this.banner = res.data.banner
      // 频道数据
      this.channel = res.data.channel
      // 品牌数据
      this.brand = res.data.brandList
      // 新品首发数据
      this.newGoodsList = res.data.newGoodsList
      // 人气推荐
      this.hotGoodsList = res.data.hotGoodsList
      // 专题精选
      this.topicList = res.data.topicList
      // 分类板块
      this.categoryList = res.data.categoryList
    }).catch(err => {
      // 请求失败之后执行
      console.log(err)
    })
  },
  methods: {
    // 产品详情页跳转
    toProductsDetail (id) {
      this.$router.push('/productsDetail?id=' + id)
    },
    // 品牌详情跳转
    toBrandList (id) {
      this.$router.push('/brandDetail?id=' + id)
    },
    toList (id) {
      this.$router.push('/list?id=' + id)
    }
  }
}
</script>

<style lang="less" scoped>
.index-container{
  padding-top: 54px;
  .title-h2{
    font-size: 18px;
    color: #333;
    text-align: center;
    padding: 25px 0 20px 0;
  }
}
.search-box{
  width: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2;
}
.slide-enter,.slide-leave-to{
  // 过渡开始之前的样式写在这里
  right: -100%;
}
.slide-enter-active,.slide-leave-active{
  // 过渡属性写在这里
  transition: right .3s;
}
.slide-enter-to,.slide-leave{
  // 过渡结束之后的样式写在这里
  right: 0;
}
.index-banner{
  width: 100%;
  .van-image{
    width: 100%;
    height: 200px;
    display: block;
  }
  /deep/ .van-swipe__indicator{
    background-color: #fff;
    opacity: .8;
  }
}
.index-channel{
   border-bottom: 10px solid #ebedf0;
   /deep/ .van-grid-item__content::after{
     display: none;
   }
  .van-image{
    width: 30px;
    height: 30px;
    display: block;
  }
  .text{
    font-size: 14px;
    color: #333;
    margin-top: 5px;
  }
}
.index-brand{
  padding-bottom: 5px;
  border-bottom: 10px solid #ebedf0;
  ul{
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li{
      width: 50%;
      padding: 5px;
      box-sizing: border-box;
      .box{
        width: 100%;
        position: relative;
      }
      .van-image{
        width: 100%;
        height: 102px;
        display: block;
      }
      .tmain{
        position: absolute;
        left: 10px;
        top: 15px;
      }
      .tit{
        font-size: 15px;
        color: #333;
        line-height: 1;
      }
      .price{
        font-size: 12px;
        color: #8b0000;
      }
    }
  }
}
.index-newGoods{
  padding-bottom: 20px;
  border-bottom: 10px solid #ebedf0;
  ul{
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    margin-top: -10px;
    li{
      width: 50%;
      padding: 5px;
      box-sizing: border-box;
      text-align: center;
      .box{
        width: 100%;
        position: relative;
      }
      .van-image{
        width: 100%;
        height: 172px;
        display: block;
      }
      .tit{
        font-size: 15px;
        color: #333;
        line-height: 1;
        margin-top: 5px;
      }
      .price{
        font-size: 12px;
        color: #8b0000;
      }
    }
  }
}
.index-hotGoods{
  padding-bottom: 20px;
  border-bottom: 10px solid #ebedf0;
  ul{
    padding: 0 10px;
    margin-top: -10px;
    li{
      margin-top: 10px;
      background-color: #fafafa;
      display: flex;
      .van-image{
        width: 88px;
        height: 88px;
        display: block;
        flex-shrink: 0;
      }
      .tmain{
        padding: 15px 10px;
        overflow: hidden;
      }
      .tit{
        font-size: 16px;
        color: #333;
      }
      .summary{
        font-size: 13px;
        color: #999;
        margin-top: 4px;
      }
      .price{
        font-size: 12px;
        color: #8b0000;
        margin-top: 15px;
        display: block;
      }
    }
  }
}
.index-special{
  padding-bottom: 20px;
  border-bottom: 10px solid #ebedf0;
  .special-list{
     overflow: hidden;
    .van-swipe{
      margin: 0 -10px;
    }
    .van-swipe-item{
      padding: 0 10px;
      box-sizing: border-box;
    }
    .van-swipe-item:last-child{
      margin-right: 0;
    }
    /deep/ .van-swipe__indicators{
      display: none;
    }
    .van-image{
      width: 100%;
      height: 200px;
      display: block;
    }
    .tmain{
      padding: 10px 10px 0 10px;
      overflow: hidden;
      box-sizing: border-box;
    }
    .tit{
      font-size: 16px;
      color: #333;
    }
    .summary{
      font-size: 13px;
      color: #999;
      margin-top: 4px;
    }
    .price{
      font-size: 12px;
      color: #8b0000;
      margin-top: 10px;
      display: block;
    }
  }
}
.index-category{
  padding-bottom: 20px;
  .title-h2::before{
    content: '';
    width: 30px;
    height: 1px;
    background-color: #ddd;
    display: inline-block;
    margin-right: 10px;
    vertical-align: middle;
  }
  .title-h2::after{
    content: '';
    width: 30px;
    height: 1px;
    background-color: #ddd;
    display: inline-block;
    margin-left: 10px;
    vertical-align: middle;
  }
  ul{
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    margin-top: -10px;
    li{
      width: 50%;
      padding: 5px;
      box-sizing: border-box;
      text-align: center;
      .box{
        width: 100%;
        position: relative;
      }
      .van-image{
        width: 100%;
        height: 172px;
        display: block;
      }
      .tit{
        font-size: 15px;
        color: #333;
        line-height: 1;
        margin-top: 5px;
      }
      .price{
        font-size: 12px;
        color: #8b0000;
      }
    }
  }
}
</style>
